package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Book;
import  dao.BookDAO;
import entity.Page;

/**
 * Servlet implementation class BookUpdateServlet
 */
@WebServlet("/updateBook")
public class BookUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	BookDAO bookDao = new BookDAO();  
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookUpdateServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		Book book = null;
		String mes = "";
		try{
			if(request.getParameter("uuid") != null){
				book = new Book();
				book.setUuid(Integer.parseInt(request.getParameter("uuid")));
				book.setBookName(request.getParameter("bookName"));
				book.setPicUrl(request.getParameter("picUrl"));
				book.setPrice(Float.parseFloat(request.getParameter("price")));
				if(bookDao.add(book) > 0){
					mes = "更新成功！";
				} else {
					mes = "更新失败!";
				}
			}
		}catch(Exception ex){
			mes = "更新失败!";
			ex.printStackTrace();
		}
		request.setAttribute("MES", mes);
		request.getRequestDispatcher("BookShow").forward(request, response);	
	}
}
